package argo.weekcontest.wk0427;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CountCoveredBuildings {


    public int countCoveredBuildings(int n, int[][] buildings) {

        int res = 0;
        if (buildings == null || buildings.length <= 4) {
            return res;
        }

        Map<Integer, int[]> xyMap = new HashMap<>();
        Map<Integer, int[]> yxMap = new HashMap<>();

        for (int[] building : buildings) {
            int x = building[0];
            int y = building[1];

            int[] yMinMaxMap = xyMap.getOrDefault(x, new int[]{n, -1});
            yMinMaxMap[0] = y < yMinMaxMap[0] ? y : yMinMaxMap[0];
            yMinMaxMap[1] = y > yMinMaxMap[1] ? y : yMinMaxMap[1];
            xyMap.put(x, yMinMaxMap);

            int[] xMinMaxMap = yxMap.getOrDefault(y, new int[]{n, -1});
            xMinMaxMap[0] = x < xMinMaxMap[0] ? x : xMinMaxMap[0];
            xMinMaxMap[1] = x > xMinMaxMap[1] ? x : xMinMaxMap[1];
            yxMap.put(y, xMinMaxMap);

        }

        for (int[] building : buildings) {
            int x = building[0];
            int y = building[1];
            //check y
            int[] yMinMaxMap = xyMap.get(x);
            int[] xMinMaxMap = yxMap.get(y);
            if (y > yMinMaxMap[0] && y < yMinMaxMap[1] && x > xMinMaxMap[0] && x < xMinMaxMap[1]) {
                res++;
                System.out.println("x: " + x + ",y:" + y);
            }
        }


        return res;
    }


    public static void main(String[] args) {
        //[[1,3],[3,2],[3,3],[3,5],[5,3]]
        //int i = new CountCoveredBuildings().countCoveredBuildings(5, new int[][]{{1, 3}, {3, 2}, {3, 3}, {3, 5}, {5, 3}});
        //int i = new CountCoveredBuildings().countCoveredBuildings(5, new int[][]{{1, 3}, {3, 2}, {3, 3}, {3, 5}, {5, 3}});
        int i = new CountCoveredBuildings().countCoveredBuildings(3, new int[][]{{1, 2}, {2, 1}, {3, 1}, {1, 1}, {3, 3}});
        System.out.println(i);
    }
}
